﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CLWMR = Cherish.LScheduler.Website.Models.RequestResult;
using CLWMC = Cherish.LScheduler.Website.Models.CommonException;
using CLD = Cherish.LScheduler.Data;

namespace Cherish.LScheduler.Website.Controllers
{
    public class SchedulerController : Controller
    {
        //
        // GET: /Scheduler/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Test()
        {
            return View();
        }


        public ActionResult Test1()
        {
            return View();
        }


        public JsonResult MyMethod()
        {
            System.Threading.Thread.Sleep(2000);
            //byte[] byts = new byte[Request.InputStream.Length];
            //Request.InputStream.Read(byts, 0, byts.Length);
            //string req = System.Text.Encoding.Default.GetString(byts);
            //req = Server.UrlDecode(req);
            return Json(new { ID = "a" });
        }

        /// <summary>
        /// 创建活动
        /// </summary>
        /// <returns></returns>
        public JsonResult CreateActivity()
        {
            try
            {
                var subject = Request.Form["subject"];
                if (string.IsNullOrWhiteSpace(subject))
                {
                    throw new CLWMC("活动主题必须输入");
                }
                if (subject.Length > 30)
                {
                    throw new CLWMC("活动主题的长度不得超过30个");
                }
                var time = Request.Form["starttime"];
                DateTime startTime;
                if (string.IsNullOrWhiteSpace(time) || !DateTime.TryParse(time, out startTime))
                {
                    throw new CLWMC("活动开始时间格式不正确");
                }
                time = Request.Form["endtime"];
                DateTime endTime;
                if (string.IsNullOrWhiteSpace(time) || !DateTime.TryParse(time, out endTime))
                {
                    throw new CLWMC("活动结束时间格式不正确");
                }
                if (endTime <= startTime)
                {
                    throw new CLWMC("活动开始时间不得大于活动结束时间");
                }
                if ((endTime - startTime).Minutes < 15)
                {
                    throw new CLWMC("活动开始时间与结束时间间隔不得小于15分钟");
                }
                CLD.IActivity activity = new CLD.Activity();
                activity.Subject = subject;
                activity.StartTime = startTime;
                activity.EndTime = endTime;
                activity.Content = Request.Form["content"] ?? "";
                activity.Color = Request.Form["color"] ?? "#5484ed";
                new CLD.Sqlite.ActivityAccessor().Create(activity);
                return Json(CLWMR.DefaultSuccessResult);
            }
            catch (CLWMC c)
            {
                return Json(new CLWMR(false, c.Message));
            }
            catch
            {
                return Json(CLWMR.DefaultErrorResult);
            }
        }


        public JsonResult GetActivityList()
        {
            var criteria = new CLD.ActivityCriterion();
            //捕捉出当天的数据
            criteria.StartTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            criteria.EndTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            return Json(new CLD.Sqlite.ActivityAccessor().GetList(criteria));
        }
    }
}
